#ifndef _DC_CAPTURA_H__
#define _DC_CAPTURA_H__

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <unistd.h>
#define _LINUX_TIME_H
#define _DEVICE_H_
#include <linux/videodev.h>

#include <libraw1394/raw1394.h>
#include <libdc1394/dc1394_control.h>
//#include "affine.h"

#include <cv.h>
#include <highgui.h>
#include "dc1394_multiview.h"

#define FALSE 0
#define TRUE  1



#define NUM_VERSION "0.1"
#define MAX_CAMERAS 8   /* Max number of cameras allowed */
#define MAX_PORTS 4     /* Max number of cards allowed */
#define MAX_RESETS 5    /* Max number of bus reset to find a camera */
#define MAX_SYNC_TRY 10 /* Max number of DMA buffer dicards to synchronize two cams */


#define MAX_FRAMES_VIDEO 999999 /* Max length in frames of a video */
#define SMALL_IMAGE_WIDTH 320 /* Width of resized images */
#define SMALL_IMAGE_HEIGHT 240 /* Height of resized images */
#define HIGH_ACTIVE_INPUT 1
#define ON_TRIGGER_MODE 1
#define OFF_TRIGGER_MODE 0
#define TRIGGER_MODE_LEVEL 1
#define TRIGGER_MODE_PROGRAMMABLE 0

#define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define BN 1
#define COLOR 3

/* typedef enum */
/* { */
/*    NOT_PRESENT = 0, */
/*    MANUAL, */
/*    AUTO, */
/*    MANUAL_AUTO */
/* } feature_available_t; */

/* typedef struct */
/* { */
/*   feature_available_t available_modes; */
/*   feature_available_t working_mode; */
/*   unsigned int min,max; */
/*   unsigned int value; */
/* }feature_t; */



typedef struct
{
  /*********    Camera Members   **********/
  raw1394handle_t handle;
  dc1394_cameracapture camera;
  FILE *pf,*pfw;
  char file_name[50];
  char windowName[50];
  char file_opts[10];
  IplImage *image; 
  IplImage *small_image;
  int numCams;      
  u_int64_t g_guid; 
  quadlet_t UID[2];
  char vendor[50];
  char model[50];
  int written_frame;
  int frame_size;
  int video_long;
  int video_offset;
  int feature_headers_size;

  /*********    Configuration Members   **********/
  int drop_frames;
  int num_dma_buffers;
  unsigned int xoffset;
  unsigned int yoffset;
  unsigned int width;
  unsigned int height;
  unsigned int width_small;
  unsigned int height_small;
  unsigned int mode;
  unsigned int format;
  unsigned int color_format;
  unsigned int channel;
  unsigned int speed;

  dc1394_feature_set feature_set;

  int frame_rate;
  int packet_size;
  char *dc_dev_name;
  int save;
  int save_size;
  int soft_resync;
  int ext_trigger;
  int resized;
  int show;
  int color;
}camera_control;

/* Quitar en cuanto se pueda la variable global */

/* camera_control cameras[MAX_CAMERAS]; */

void default_options(camera_control *);
void get_options(int argc,char *argv[], camera_control *);
void cleanup(camera_control *);

int dc_init(camera_control *);
int dc_config(camera_control *);
int dc_start_iso(camera_control *);
int dc_cameras_resync(camera_control *, int, int);
int dc_capture(camera_control *, int);
int dc_create_save_header(camera_control *, int, int );
int dc_close_files(camera_control *, int ,int);
int dc_init_images(camera_control *cameras, int camNum);
int dc_create_window(camera_control *cameras, int camNum);
int dc_save_image(camera_control *, int, int, int);
int dc_show_image(camera_control *, int);

int dc_get_features(camera_control *, int );
int dc_set_feature(camera_control *, int , int , int );
int dc_set_features(camera_control *, int );
int dc_print_features(camera_control *, int );
int dc_print_single_feature(dc1394_feature_info *);
int dc_check_feature_limits(dc1394_feature_info *, int );
int dc_trigger_external_mode_on (camera_control *cameras, int camNum);
int dc_trigger_external_mode_off (camera_control *cameras, int camNum);


int dc_auto_on_off_feature(camera_control *, int , int ,int );
int dc_on_off_feature(camera_control *, int , int ,int);


int dc_set_brightness(camera_control *, int , int );
int dc_auto_on_off_brightness(camera_control *, int , int );
int dc_set_exposure(camera_control *, int , int );
int dc_auto_on_off_exposure(camera_control *, int , int );
int dc_set_white_balance(camera_control *, int , int );
int dc_auto_on_off_white_balance(camera_control *, int , int );

int dc_main_loop(camera_control *, int , char );


/***************** Parte del read file *******************/

int dc_get_filenames(camera_control *, int , char **);
int dc_read_save_header(camera_control *, int );
int dc_print_video_header(camera_control *, int );
int dc_read_frame(camera_control *, int );
int dc_print_capture_param(camera_control *, int );
int dc_main_read_loop(camera_control *, int , char );
int dc_seek_file_pos(camera_control *, int , int );


#endif
